<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="电子证书状态" prop="digitalStatus">
        <el-select v-model="queryParams.digitalStatus" placeholder="请选择电子证书状态" clearable>
          <el-option
            v-for="dict in dict.type.digital_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="纸质证书状态" prop="paperStatus">
        <el-select v-model="queryParams.paperStatus" placeholder="请选择纸质证书状态" clearable>
          <el-option
            v-for="dict in dict.type.paper_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="证书编号" prop="certificateNo">
        <el-input
          v-model="queryParams.certificateNo"
          placeholder="请输入证书编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子证书发布时间" prop="digitalIssueTime">
        <el-date-picker clearable
          v-model="queryParams.digitalIssueTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择电子证书发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="证书有效期起" prop="validityStart">
        <el-date-picker clearable
          v-model="queryParams.validityStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择证书有效期起">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="证书有效期止" prop="validityEnd">
        <el-date-picker clearable
          v-model="queryParams.validityEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择证书有效期止">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="纸质证书发放时间" prop="paperIssueTime">
        <el-date-picker clearable
          v-model="queryParams.paperIssueTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择纸质证书发放时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="纸质证领取方式" prop="receiveMethod">
        <el-select v-model="queryParams.receiveMethod" placeholder="请选择纸质证领取方式" clearable>
          <el-option
            v-for="dict in dict.type.receive_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号" prop="trackingNo">
        <el-input
          v-model="queryParams.trackingNo"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否立刻发布电子证书成绩(0:否,1:是)" prop="immediatePublish">
        <el-select v-model="queryParams.immediatePublish" placeholder="请选择是否立刻发布电子证书成绩(0:否,1:是)" clearable>
          <el-option
            v-for="dict in dict.type.immediate_publish"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="撤销原因" prop="revokeReason">
        <el-input
          v-model="queryParams.revokeReason"
          placeholder="请输入撤销原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="临近过期期限(天)" prop="expireReminderDays">
        <el-select v-model="queryParams.expireReminderDays" placeholder="请选择临近过期期限(天)" clearable>
          <el-option
            v-for="dict in dict.type.expire_reminder_days"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['training:certificateIssue:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['training:certificateIssue:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['training:certificateIssue:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['training:certificateIssue:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="certificateIssueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="证书发放管理id" align="center" prop="certificateIssueId" />
      <el-table-column label="证书模板id" align="center" prop="templateId" />
      <el-table-column label="学员id" align="center" prop="studentId" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="报名id" align="center" prop="registrationId" />
      <el-table-column label="证书类型" align="center" prop="certificateType" />
      <el-table-column label="电子证书状态" align="center" prop="digitalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.digital_status" :value="scope.row.digitalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="纸质证书状态" align="center" prop="paperStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.paper_status" :value="scope.row.paperStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="证书编号" align="center" prop="certificateNo" />
      <el-table-column label="电子证书发布时间" align="center" prop="digitalIssueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.digitalIssueTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证书有效期起" align="center" prop="validityStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证书有效期止" align="center" prop="validityEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="纸质证书发放时间" align="center" prop="paperIssueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paperIssueTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="纸质证领取方式" align="center" prop="receiveMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.receive_method" :value="scope.row.receiveMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="trackingNo" />
      <el-table-column label="是否立刻发布电子证书成绩(0:否,1:是)" align="center" prop="immediatePublish">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.immediate_publish" :value="scope.row.immediatePublish"/>
        </template>
      </el-table-column>
      <el-table-column label="撤销原因" align="center" prop="revokeReason" />
      <el-table-column label="临近过期期限(天)" align="center" prop="expireReminderDays">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.expire_reminder_days" :value="scope.row.expireReminderDays"/>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['training:certificateIssue:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['training:certificateIssue:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书发放管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="证书模板id" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入证书模板id" />
        </el-form-item>
        <el-form-item label="学员id" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学员id" />
        </el-form-item>
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="报名id" prop="registrationId">
          <el-input v-model="form.registrationId" placeholder="请输入报名id" />
        </el-form-item>
        <el-form-item label="电子证书状态" prop="digitalStatus">
          <el-select v-model="form.digitalStatus" placeholder="请选择电子证书状态">
            <el-option
              v-for="dict in dict.type.digital_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="纸质证书状态" prop="paperStatus">
          <el-select v-model="form.paperStatus" placeholder="请选择纸质证书状态">
            <el-option
              v-for="dict in dict.type.paper_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证书编号" prop="certificateNo">
          <el-input v-model="form.certificateNo" placeholder="请输入证书编号" />
        </el-form-item>
        <el-form-item label="电子证书发布时间" prop="digitalIssueTime">
          <el-date-picker clearable
            v-model="form.digitalIssueTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择电子证书发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证书有效期起" prop="validityStart">
          <el-date-picker clearable
            v-model="form.validityStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择证书有效期起">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证书有效期止" prop="validityEnd">
          <el-date-picker clearable
            v-model="form.validityEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择证书有效期止">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="纸质证书发放时间" prop="paperIssueTime">
          <el-date-picker clearable
            v-model="form.paperIssueTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择纸质证书发放时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="纸质证领取方式" prop="receiveMethod">
          <el-select v-model="form.receiveMethod" placeholder="请选择纸质证领取方式">
            <el-option
              v-for="dict in dict.type.receive_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNo">
          <el-input v-model="form.trackingNo" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="是否立刻发布电子证书成绩(0:否,1:是)" prop="immediatePublish">
          <el-select v-model="form.immediatePublish" placeholder="请选择是否立刻发布电子证书成绩(0:否,1:是)">
            <el-option
              v-for="dict in dict.type.immediate_publish"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="撤销原因" prop="revokeReason">
          <el-input v-model="form.revokeReason" placeholder="请输入撤销原因" />
        </el-form-item>
        <el-form-item label="临近过期期限(天)" prop="expireReminderDays">
          <el-select v-model="form.expireReminderDays" placeholder="请选择临近过期期限(天)">
            <el-option
              v-for="dict in dict.type.expire_reminder_days"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable
            v-model="form.updatedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCertificateIssue, getCertificateIssue, delCertificateIssue, addCertificateIssue, updateCertificateIssue } from "@/api/training/certificateIssue"

export default {
  name: "CertificateIssue",
  dicts: ['paper_status', 'expire_reminder_days', 'receive_method', 'immediate_publish', 'digital_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书发放管理表格数据
      certificateIssueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certificateType: null,
        digitalStatus: null,
        paperStatus: null,
        certificateNo: null,
        digitalIssueTime: null,
        validityStart: null,
        validityEnd: null,
        paperIssueTime: null,
        receiveMethod: null,
        trackingNo: null,
        immediatePublish: null,
        revokeReason: null,
        expireReminderDays: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询证书发放管理列表 */
    getList() {
      this.loading = true
      listCertificateIssue(this.queryParams).then(response => {
        this.certificateIssueList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        certificateIssueId: null,
        templateId: null,
        studentId: null,
        projectId: null,
        registrationId: null,
        certificateType: null,
        digitalStatus: null,
        paperStatus: null,
        certificateNo: null,
        digitalIssueTime: null,
        validityStart: null,
        validityEnd: null,
        paperIssueTime: null,
        receiveMethod: null,
        trackingNo: null,
        immediatePublish: null,
        revokeReason: null,
        expireReminderDays: null,
        createdBy: null,
        updatedBy: null,
        createdTime: null,
        updatedTime: null,
        remark: null,
        exampleOne: null,
        exampleTwo: null,
        exampleThree: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.certificateIssueId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加证书发放管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const certificateIssueId = row.certificateIssueId || this.ids
      getCertificateIssue(certificateIssueId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改证书发放管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.certificateIssueId != null) {
            updateCertificateIssue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCertificateIssue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const certificateIssueIds = row.certificateIssueId || this.ids
      this.$modal.confirm('是否确认删除证书发放管理编号为"' + certificateIssueIds + '"的数据项？').then(function() {
        return delCertificateIssue(certificateIssueIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('training/certificateIssue/export', {
        ...this.queryParams
      }, `certificateIssue_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
